<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class category_model extends WST_Model {
	private $_table = "category";

	function __contruct(){
        parent::__construct();
    }

	public function getSingle($id=null){
		$data= array();
		$sql = "SELECT * FROM ".$this->_table." WHERE id={$id} ";
		$query = $this->db->query($sql);
		$data = $query->row_array();
		$query->free_result();
		return $data;
	}

	function count_all($param=array()){
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $this->db->like('name',$param['keyword']);
            }
            if(!empty($param['parent'])){
                $this->db->where('parent', $param['parent']);
            }
            if(!empty($param['child'])){
                $this->db->where('parent', $param['child']);
                $this->db->where('level', $param['parent']);
            }
        }
		 return $this->db->count_all_results('category');
	}

    function find_all($param=array(),$current_page = 1, $page_size = 30){

        $where =" WHERE 1=1";
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $where.=" AND name LIKE '%".$param['keyword']."%'";
            }
            if(!empty($param['parent'])){
                $where.=" AND parent = ".$param['parent']."";
            }
            if(!empty($param['child'])){
                $where.=" AND  parent = ".$param['child']." AND level= ".$param['parent']." ";
            }
        }
        $sql = "select * from ".$this->_table. $where."   order by id ASC";

        if ($current_page > 0 && $page_size > 0) {
            $offset = ($current_page-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }
        $query = $this->db->query($sql);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

	public function getListAll(){
		$sql = "SELECT * FROM ".$this->_table." WHERE  status=1 ORDER BY orderNo ASC ";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function getListLevel($level=0){
        $this->db->where('parent',$level);
        $query = $this->db->get($this->_table);
        $data = $query->result_array();
        $query->free_result();
        return $data;
	}

	public function getCategory($type=''){
		$sql = "SELECT id,name FROM ".$this->_table." WHERE type='{$type}' AND status=1 ORDER BY orderNo ASC";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function getSubCategory($id=null){
		$sql = "SELECT id,name,type FROM ".$this->_table." WHERE parent='{$id}' AND status=1 ORDER BY orderNo ASC";

		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function insert($data=array()){
		if($this->db->insert($this->_table,$data))
			return $this->db->insert_id();
		else
			return FALSE;
	}

	public function update($data,$id=null){
		$this->db->where("id",$id);
		if($this->db->update($this->_table,$data))
			return TRUE;
		else
			return FALSE;
	}

	function getShowCategroy($level){
		$sql = "SELECT * FROM ".$this->_table." WHERE parent='{$level}' AND status=1 ORDER BY orderNo ASC";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function all_cats($flag=false){
		$all_category = $this->cache->get('all_cats');
		if($all_category === false || $flag===true){
			$this->db->where('status',1);
			$this->db->order_by('orderNo','ASC');
			$query = $this->db->get($this->_table);
			$data = $query->result_array();
            $query->free_result();
            $all_category=array();
            foreach($data as $val){
              $all_category[$val['id']]=$val;
            }

			$this->cache->save('all_cats', $all_category);
		}
		return $all_category;
	}

//    public function all_cats_type($cat_id,$type,$flag=false){
//        $all_needs = $this->cache->get('all_needs_'.$cat_id.'_'.$type);
//        if($all_needs===false || $flag===true){
//            $this->db->where('parent',$cat_id);
//            $this->db->where('type',$type);
//            $this->db->where('status',1);
//            $query = $this->db->get($this->_table);
//            $all_needs= $query->result_array();
//            $query->free_result();
//            $this->cache->save('all_needs_'.$cat_id.'_'.$type, $all_needs);
//        }
//        return  $all_needs;
//    }
   public function delete($ids){
       if(is_array($ids)){
           foreach($ids as $id){
               $this->db->where('id',$id);
               $this->db->delete($this->_table);
           }
       }else {
           $this->db->where('id',$ids);
           $this->db->delete($this->_table);
       }
       return true;
   }


}
